﻿using Dm;
using FCT551.Helper;
using FCT551.Models;
using FCT551.ViewModels;
using NLog.LayoutRenderers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using static FCT551.Helper.MESServiceClient;

namespace FCT551.Views
{
    /// <summary>
    /// MesConfigView.xaml 的交互逻辑
    /// </summary>
    public partial class MesConfigView : UserControl
    {
        public static updateHandler updatemessage;

        public MesConfigView()
        {
            InitializeComponent();
        }

        private List<TerminalInfo> Datas;
        private List<string> TerminalNames;
        private List<string> TerminalOrders;

        /// <summary>
        /// 查询所有工站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void CheckAllWorkSation(object sender, RoutedEventArgs e)
        {
            await Askinfo();
        }

        public async Task Askinfo()
        {
            TerminalListResponse TerminalListResponse = await Global._MESServiceClient.GetTerminalListAsync();
            if (TerminalListResponse.IsSuccess)
            {
                Datas = new();
                Datas = TerminalListResponse.Data;
                List<string> Lines = Datas.Select(s => s.ProductionLineCode).ToList();

                LineNumbers.ItemsSource = Lines;

                //根据选择的线体名称，返回对应的工站表和工单表

                string name = LineNumbers.SelectedItem.ToString();
                var lists = Datas.Where(s => s.ProductionLineCode == name).ToList();
                TerminalNames = lists[0].Terminals;
                WorkStationNames.ItemsSource = TerminalNames;
                var Orders = lists[0].WorkOrders;
                var odderslist = Orders.Select(s => $"工单料号：{s.PartNumber},机种名称：{s.PartName},工单号：{s.WorkOrder}").ToList();
                WorkOrders.ItemsSource = odderslist;
                WorkOrders.SelectedIndex = 0;
                WorkStationNames.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("查询失败！！");
            }
        }

        /// <summary>
        /// 保存当前工单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save(object sender, RoutedEventArgs e)
        {
            Global._WorkStationName = WorkStationNames.SelectedItem.ToString();
            string ordermsg = WorkOrders.SelectedItem.ToString().Split(',')[2].Remove(0, 4);
            Global._WorkStationOrderName = ordermsg;
            //写入xml文件
            ConfigsModel configsModel = new();
            configsModel.Users = Global._Users;
            configsModel.Configs = Global._Configs;
            configsModel.OrderName = Global._WorkStationOrderName;
            configsModel.WorkStationName = Global._WorkStationName;
            var serializer = new XmlSerializer(typeof(ConfigsModel));
            using (var writer = new StreamWriter(Global._ConfigPath))
            {
                serializer.Serialize(writer, configsModel);
            }
            UpdateMessage("保存成功！");
        }

        private void UpdateMessage(string msg, LogLevel logLevel = LogLevel.Info)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                updatemessage?.Invoke(msg, logLevel);
            });
        }

        /// <summary>
        /// 当选择线体变化时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LineNumbers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //根据选择的线体名称，返回对应的工站表和工单表

            string name = LineNumbers.SelectedItem.ToString();
            var lists = Datas.Where(s => s.ProductionLineCode == name).ToList();
            TerminalNames = lists[0].Terminals;
            WorkStationNames.ItemsSource = TerminalNames;
            var Orders = lists[0].WorkOrders;
            var odderslist = Orders.Select(s => $"工单料号：{s.PartNumber},机种名称：{s.PartName},工单号：{s.WorkOrder}").ToList();
            WorkOrders.ItemsSource = odderslist;
            WorkOrders.SelectedIndex = 0;
            WorkStationNames.SelectedIndex = 0;
        }
    }
}